#!/usr/bin/env python
# coding: utf-8

import gtk
import os
import random
import copy
import cPickle as pickle

#from playsound import read, play

def show_info(message, title = "提示", buttons = gtk.BUTTONS_OK, parent = None):
    dialog = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, buttons)
    dialog.set_icon_from_file("../glade/wordjoy.png")
    dialog.set_title(title)
    dialog.set_markup(message)
    dialog.run()
    dialog.destroy()

class MessageDialog(gtk.MessageDialog):

    def __init__(self, 
            message,
            title = "提示",
            parent = None, 
            flags = 0, 
            type = gtk.MESSAGE_INFO,
            buttons = gtk.BUTTONS_YES_NO):
        gtk.MessageDialog.__init__(self, parent, flags, type, buttons)
        self.set_markup(message)
        self.set_title(title)
        self.set_icon_from_file("wordjoy.png")

class WordReview(gtk.EventBox):
    def __init__(self, rr, finish_cb):
        gtk.EventBox.__init__(self)

        self.rr = rr
        self.finish_cb = finish_cb
        self.passed = False
        self.queue = []

        vbox = gtk.VBox(False, 0)
        vbox.show()
        self.add(vbox)

        self.title = gtk.Label()
        self.title.show()
        vbox.pack_start(self.title, False, False, 20)

        hbox = gtk.HBox(False, 0)
        hbox.show()
        vbox.pack_start(hbox, False, False, 0)

        button1 = gtk.Button("Hello")
        button1.show()
        button1.connect("clicked", self.toggled_cb)
        hbox.pack_start(button1, True, True, 5)

        button2 = gtk.Button("Hello")
        button2.show()
        button2.connect("clicked", self.toggled_cb)
        hbox.pack_start(button2, True, True, 5)

        button3 = gtk.Button("Hello")
        button3.show()
        button3.connect("clicked", self.toggled_cb)
        hbox.pack_start(button3, True, True, 5)

        button4 = gtk.Button("Hello")
        button4.show()
        button4.connect("clicked", self.toggled_cb)
        hbox.pack_start(button4, True, True, 5)

        hbox = gtk.HBox(False, 0)
        hbox.show()
        vbox.pack_start(hbox, False, False, 10)

        self.status = gtk.Label()
        self.status.show()
        hbox.pack_end(self.status, False, False, 0)

        self.buttons = [button1, button2, button3, button4]
        self.connect("button_press_event", self.button_press_event)

    def start_review(self, rr):
        """初始化词意回想，采用随机采样方式，将单词丢进池子"""
        self.rr = rr
        self.passed = False
        self.queue = []
        self.pool = copy.copy(self.rr.words)
        self.next_word()

    def button_press_event(self, widget, event, data = None):
        if self.passed == True:
            self.next_word()
            self.passed = False

        return False
                
    def remain_list(self):
        remain = self.rr.get_dict().keys()
        remain.remove(self.now)
        return remain

    def toggled_cb(self, widget, data = None):
        cn = widget.get_label()
        lable = self.rr.get_dict()[self.now]
        if cn == lable:
            self.queue.append(self.now)
            play("answerok")
            if len(self.queue) < len(self.rr.words):
                self.status.set_label("回答正确！点击空白处继续.")
                self.passed = True
            else:
                show_info("回想完毕.请等待下次复习！")
                self.finish_cb(None)
        else:
            play("answerno")
            self.status.set_label("回答错误！请重选.")

    def next_word(self):
        self.now = random.sample(self.pool, 1)[0]
        self.pool.remove(self.now)
        read(self.now)
        self.title.set_markup('<span size="x-large">%s</span>' % self.now)
        self.status.set_label("第%d个(共%d个)" % (len(self.queue) + 1, len(self.rr.words)))

        answer = random.randint(0, 3)
        self.buttons[answer].set_label(self.rr.get_dict()[self.now])

        filling = random.sample(self.remain_list(), 4)
        for button in self.buttons:
            if self.buttons.index(button) != answer:
                button.set_label(self.rr.get_dict()[filling[self.buttons.index(button)]])

if __name__ == "__main__":
    from reciterecord import ReciteRecord
    win = gtk.Window()

    win.connect('destroy', lambda *w: gtk.main_quit())
    win.set_title("Widget Test")
    win.set_default_size(450, 300)
    win.set_border_width(8)

    vbox = WordReview(None, None)
    vbox.start_review(ReciteRecord("books/qqssbdc/cykych/ck-kq.bok"))
    vbox.show()
    win.add(vbox)

    win.show()
    gtk.main()
